<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BgcmsMultiLanguageBean {
		
		public function __construct() {
			
		}
    	
		public function get_active_lang($db,$id) {
			$sql = "SELECT * " .
				   "FROM " . TBL_LANGUAGE_SETTING . " l ".
				   "WHERE l.id = '$id'";
			$result = $db->get_row($sql);
			
			return $result;
			
		}
		
		public function update_active_lang($db,$id,$lang_active,$langid) {
			$sql = sprintf("UPDATE  %s SET lang_active  = '%s',lang_id  = '%s'" .
    					" WHERE id = '%s'", 
					TBL_LANGUAGE_SETTING,
					$lang_active,
					$langid,
					$id);
			$res = $db->query($sql);
			return $res;
		}
		public function save_lang($db,$lang) {
			if($lang['name_lang'] != "" && $lang['key_lang'] != "") { 
			$sql = $db->query(sprintf("INSERT INTO " . TBL_LANGUAGE_LIST . " (key_lang, name_lang, flags, active) " .
					"values ('%s', '%s', '%s', '%s')", 
					$lang['key_lang'], 
					$lang['name_lang'], 
					$lang['flag'], 
					$lang['active']));
			$id =  $db->insert_id;		
			$db->query(sprintf("ALTER TABLE " . TBL_MULTI_LANGUAGE . " ADD " . $lang['name_lang'] . " LONGTEXT NULL"));
			return $id;
			}
			
		}
		
		public function del_lang($db,$namelang,$keylang) 
		{
			$sql = $db->query("DELETE FROM " . TBL_LANGUAGE_LIST . " WHERE key_lang= '$keylang'");
			$db->query(sprintf(" ALTER TABLE " . TBL_MULTI_LANGUAGE . " DROP " . $namelang . "  "));
		}
		
		public function update_lang($db,$lang,$name_old,$name_check) {
			$sql = $db->query(sprintf("UPDATE " . TBL_LANGUAGE_LIST . " SET " .
				"key_lang = '%s',name_lang = '%s',flags = '%s',active = '%s' WHERE " .
				"lang_id = '%s'", $lang['key_lang'], $lang['name_lang'], $lang['flag'],$lang['active'], $lang['lang_id']));
				if($name_check=='1')
					$db->query(sprintf(" ALTER TABLE " . TBL_MULTI_LANGUAGE . " CHANGE " . $name_old . " " . $lang['name_lang'] . " LONGTEXT CHARACTER SET latin1 COLLATE utf8_general_ci NULL DEFAULT NULL  "));	
				
		}
		
		public function get_lang_byID($db,$id) {
			$sql = "SELECT * " .
				   "FROM " . TBL_LANGUAGE_LIST . " l ".
				   "WHERE l.lang_id = '$id'";
			$result = $db->get_row($sql);
			
			return $result;
			
		}
		
		public function get_lang_bykey($db,$key) {
			$sql = sprintf("SELECT * " .
				   "FROM " . TBL_LANGUAGE_LIST . " l ".
				   "WHERE l.key_lang = '$key'");
			$result = $db->get_row($sql);
			
			return $result;
			
		}
		
		public function get_lang_byname($db,$key) {
			$sql = sprintf("SELECT * " .
				   "FROM " . TBL_LANGUAGE_LIST . " l ".
				   "WHERE l.name_lang = '$key'");
			$result = $db->get_row($sql);
			
			return $result;
			
		}
		
		public function get_all_lang($db) {
		
			$sql = sprintf("SELECT * " .
				   "FROM " . TBL_LANGUAGE_LIST . " l ".
				   "ORDER BY l.lang_id ");
			$result = $db->get_results($sql);
			
			return $result;
			
		}
		
		
		public function save_translator_lang($db,$lang) {
			$alllang = BgcmsMultiLanguageBean::get_all_lang($db);
			$str="";
			$strlang="";	
			foreach($alllang as $item)
			{
				$name = $item->name_lang;
				$str .= " ".$name.",";
				$strlang .= " '".$lang[$name]."',";
				$multi = $lang['multilanguage'];
				$email = $lang['for_email'];
			}
			$sql = $db->query(sprintf("INSERT INTO " . TBL_MULTI_LANGUAGE . 
					" (language_const_id, ".$str." for_email) " .
					"values ('$multi', ".$strlang." '$email')"));
			
		}
		
		public function del_translator_lang($db,$lang) {
			$sql = $db->query("DELETE FROM " . TBL_MULTI_LANGUAGE . " WHERE language_const_id = '$lang'");
		}
		
		public function view_lang($db,$for_email,$sort,$search) {
			if($for_email!="")
				$cond = "WHERE for_email='$for_email'" ;
			$sql = sprintf("SELECT * " .
				   "FROM " . TBL_MULTI_LANGUAGE . " l $cond $search".
				   "ORDER BY l.language_const_id $sort");
			$result = $db->get_results($sql);
			
			return count($result);
			
		}
		
		
		public function view_lang_paging($db,$paging,$path,$for_email,$sort,$search) {
			$page = isset ( $_GET[$path] ) ? intval ( $_GET[$path] ) : 1;
			$rows_per_page= $paging;
			$page_start = ( $page - 1 ) * $rows_per_page;
			$page_end = $page * $rows_per_page;
			if($for_email!="")
				$cond = "WHERE l.for_email='$for_email'" ;
			$sql = "SELECT * " .
				   "FROM " . TBL_MULTI_LANGUAGE . " l $cond $search".
				   "ORDER BY l.language_const_id $sort LIMIT $page_start, $rows_per_page";
			$result = $db->get_results($sql);
			
			return $result;
			
		}
		
		public function view_translator_bykey($db,$key) {
			$sql = "SELECT * " .
				   "FROM " . TBL_MULTI_LANGUAGE . " l ".
				   "WHERE l.language_const_id = '$key'";
			$result = $db->get_row($sql);
			
			return $result;
			
		}
		
				
		public function update_translator($db,$lang) 
		{
			$alllang = BgcmsMultiLanguageBean::get_all_lang($db);
			$str="";
			$strlang="";
			$dem=0;	
			foreach($alllang as $item)
			{
				$name = $item->name_lang;
					if($dem==0)
						$str .= " ".$name."='".$lang[$name]."'";
					else
						$str .= " , ".$name."='".$lang[$name]."'";
					$dem++;		
			}
			$multi = $lang['multilanguage'];
			$sql = sprintf("UPDATE " . TBL_MULTI_LANGUAGE . " SET " .
				" ".$str." WHERE " .
				"language_const_id = '$multi'");
			$db->query($sql);	
		}
		
		public function get_list_lang() {
			$data = array();
			$sql = mysql_query("SELECT * FROM " . TBL_LANGUAGE_LIST." l WHERE l.active='1'");
			if($sql!="")
			{
				while ($row = mysql_fetch_array($sql)) {
					$data[] = $row;
				}
			}
			return $data;
    	}
		
		public function getnamelang_bykeylang($key) {
			$sql = "SELECT * " .
				   "FROM " . TBL_LANGUAGE_LIST . " l ".
				   "WHERE l.key_lang = '$key'";
			$result = mysql_query($sql);
			
			return mysql_fetch_array($result);
			
		}
	}
	
?>